CROSS_COMPILE?=
HOST?=

CC := $(CROSS_COMPILE)gcc

export TOP=$(PWD)
export PROCESSOR_COUNT := $(shell cat /proc/cpuinfo | grep "processor" | wc -l)
export CROSS_COMPILE HOST CC

Q:=@
ifeq ("$(origin V)", "command line")
ifeq ($(V),1)
Q :=
endif
endif

OUTPUT_PATH:=$(TOP)/output
LIB_SRC_PATH:=$(TOP)/3rd/editline
LIB_PATH:=$(TOP)/output/editline
SRC_PATH:=$(TOP)/src
SRC_CORE:=$(TOP)/src/ecall
CFLAGS:=-g -Wall -O2 -ldl -lpthread -I $(TOP)/include -I $(LIB_PATH)/include

$(info USE $(CC))
$(info YOU CAN USE $(PROCESSOR_COUNT) CPUS  TO COMPILE)

all: ecall pcall

ecall: editline
	$(CC) $(wildcard $(SRC_PATH)/ecall/*.c) $(wildcard $(SRC_PATH)/common/*.c)  $(CFLAGS) -L $(LIB_PATH)/lib -l editline -o $(OUTPUT_PATH)/$@

pcall: editline
	$(CC) $(wildcard $(SRC_PATH)/pcall/*.c) $(wildcard $(SRC_PATH)/common/*.c)  $(CFLAGS) -L $(LIB_PATH)/lib -l editline -o $(OUTPUT_PATH)/$@

editline:
	$(Q)if [ -e  "$(LIB_PATH)" ];then \
		echo "$@ HAS BEEN BUILDED !!!";\
	else \
		cd $(LIB_SRC_PATH);\
		./autogen.sh;\
		./configure CC=$(CC) --prefix=$(LIB_PATH) --host=$(HOST);\
		make all -j $(PROCESSOR_COUNT);\
		make install;\
		rm $(LIB_PATH)/lib/*.so*;\
	fi

clean:
	$(Q)make -C $(LIB_SRC_PATH) clean
	$(Q)rm -r $(TOP)/output

install:
	$(Q)cp $(TOP)/output/ecall /usr/bin
	$(Q)cp $(TOP)/output/pcall /usr/bin


.PHONY: editline ecall pcall clean install